package com.irdstudio.apicenter.gateway.web.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.irdstudio.apicenter.gateway.comm.MaccsPacket;
import com.irdstudio.apicenter.gateway.comm.MaccsPacketUtil;
import com.irdstudio.apicenter.gateway.core.jdbc.MaccsBaseDAO;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;
import com.irdstudio.apicenter.gateway.web.MaccsException;
import com.irdstudio.apicenter.gateway.web.MaccsTradeDefineVo;
import com.irdstudio.apicenter.gateway.web.view.JsonView;
import com.irdstudio.apicenter.gateway.web.view.View;

/**
 * 获取公告数据，暂时每次只返回最新的10条
 * 
 * @author fmf
 *
 */
@Controller
public class AppNoticeList implements IController {
	private static final String queryNoticeSql = "select * from app_notice_show where app_id=? and notice_date<=? and notice_end_date>=? and rownum <=10 order by notice_date desc";

	@Override
	public View execute(MaccsTradeDefineVo tradeDefine, MaccsPacket inPacket, HttpServletRequest request,
			HttpServletResponse response) throws MaccsException {
		// TODO Auto-generated method stub
		String appId = inPacket.getValue("appId");

		String noeDate = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		// 获取APP用户数据权限
		MaccsBaseDAO jdbcDao = BeanUtils.getBean(MaccsBaseDAO.class);
		List<Map<String, Object>> noticeList = jdbcDao.findMapList(queryNoticeSql,
				new Object[] { appId, noeDate, noeDate });
		if (noticeList.size() < 1) {
			return new JsonView(request, response, MaccsPacketUtil.makeFailurePacket("晋商消费金融公司隆重开业。"));
		} else {
			MaccsPacket outPacket = MaccsPacketUtil.makeSimpleSuccessPacket("OK!");
			outPacket.addVariable("notice", noticeList);
			return new JsonView(request, response, outPacket);
		}

	}

}
